<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mslide extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getSlideDB($number,$offset)
    {
        $id = $this->getParamUri(3,2);
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT * FROM slide WHERE 1=1 ";
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND title like '%".$key."%'";
        }
        $sql .= "ORDER BY id DESC";
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
        
    }
    
    public function getCate($id = 0)
    {
        $data = $this->db->query('SELECT * FROM categories ORDER BY order_by ASC')->result_object();
        $html  = '';
        foreach($data AS $el=>$le)
        {
            if($id == $le->id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->title.'</option>';
        }
        return $html;
    }
    
    public function editSlideDb($id,$img)
    {
        $namePro            = $this->getParamString('namePro');
        $brief            = $this->getParamString('content');
        $link            = $this->getParamString('link');
        
        $data = array(
                    'title'             => $namePro,
                    'brief'       => $brief,
                    'link'         => $link,
                    );
        if(!in_array($img, array(null, '', '0')))
        {
            $data['img'] = $img;
            // X? lý thêm ph?n xóa ?nh
        }
        
        $this->db->update('slide', $data,"id = ".$id."");
        return 200;
    }
    
    public function getNamecate()
    {
        return $this->db->query("SELECT * FROM categories ORDER BY order_by ASC")->result_object();
    }
    
    public function addSlideDb($img)
    {
        $namePro            = $this->getParamString('namePro');
        $brief            = $this->getParamString('content');
        $link            = $this->getParamString('link');
        
        $data = array(
                    'title'             => $namePro,
                    'brief'       => $brief,
                    'link'         => $link,
                    'img'               => $img
                    );
        
        $this->db->insert('slide', $data);
        return 200;
    }
    
    
    public function getSlideEdit($id)
    {
        $var = $this->db->query('SELECT * FROM slide WHERE id = '.$id)->row_object();
        return $var;
    }
    
    public function deleteslideDB()
    {
        $id = $this->getParamInt('id');
        $this->db->delete('slide',"id = '".$id."'");
        return 200;
    }
    
    
    public function getNameSlide()
    {
        return $this->db->query("SELECT * FROM slide ORDER BY order_by ASC")->result_object();
    }
    
    public function sortsaveDB($id_list)
    {
        foreach($id_list AS $key=>$val)
        {
            $this->db->update('slide', array('order_by' => $key), "id = '".$val."'");   
        }
        return 200;    
    }
    
    
}